package com.czk.springcloud.header;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * 给每个外部经过网关的请求的url的头部加个from=public的header,对此进行判断是外部的请求还是内部的请求
 * 然后微服务通过注解来判断是否有这个头部
 *
 * @Author:ChenZhangKun
 * @Date: 2022/2/5 21:20
 */
@Component
public class HeaderAuthFilter implements GlobalFilter {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        // 每个header头上加个public
        return chain.filter(exchange.mutate().request(
                exchange.getRequest().mutate().header("id", "")
                        .header("from", "public").build()
        ).build());
    }
}
